﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

using Jarvis.Analytics;
using Jarvis.Sensors;

namespace Jarvis.Windows
{
    public partial class SensorWindow : Window
    {
        private Boolean SensorListInited = false;

        protected override void OnActivated(EventArgs e)
        {
            if (!SensorListInited)
            {
                SensorNotifier.Instance.setDispatcher(Dispatcher.CurrentDispatcher);

                sensorList.ItemsSource = SensorNotifier.Instance.updates.ObservableCollection;

                ((Jarvis.App)System.Windows.Application.Current).sensorWindow = this;

                SensorNotifier.Instance.SensorsUpdate += new SensorNotifier.SensorsUpdatedHandler(this.SensorsUpdated);

                SensorListInited = true;
            }
        }

        GridViewColumnHeader _lastHeaderClicked = null;
        ListSortDirection _lastDirection = ListSortDirection.Ascending;

        void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
            ListSortDirection direction;

            if (headerClicked != null)
            {
                if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
                {
                    if (headerClicked != _lastHeaderClicked)
                        direction = ListSortDirection.Ascending;
                    else
                    {
                        if (_lastDirection == ListSortDirection.Ascending)
                            direction = ListSortDirection.Descending;
                        else
                            direction = ListSortDirection.Ascending;
                    }

                    string header = headerClicked.Content.ToString(); // headerClicked.Column.Header as string;

                    if (header.Equals("Sensor"))
                        header = "name";
                    else if (header.Equals("Value"))
                        header = "objectString";

                    Sort(header, direction);

                    if (direction == ListSortDirection.Ascending)
                        headerClicked.Column.HeaderTemplate = Resources["HeaderTemplateArrowUp"] as DataTemplate;
                    else
                        headerClicked.Column.HeaderTemplate = Resources["HeaderTemplateArrowDown"] as DataTemplate;

                    // Remove arrow from previously sorted header
                    if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
                        _lastHeaderClicked.Column.HeaderTemplate = null;

                    _lastHeaderClicked = headerClicked;
                    _lastDirection = direction;
                }
            }
        }

        private void Sort(string sortBy, ListSortDirection direction)
        {
            ICollectionView dataView = CollectionViewSource.GetDefaultView(sensorList.ItemsSource);

            dataView.SortDescriptions.Clear();
            SortDescription sd = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sd);
            dataView.Refresh();
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                (DispatcherOperationCallback)delegate(object o)
                {
                    Hide();
                    return null;
                }, null);

            e.Cancel = true;

            AnalyticsManager.LogEvent("Window Closing", "Sensors");
        }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);

            GridViewColumnCollection columns = ((GridView)sensorList.View).Columns;

            GridViewColumn sensorsColumn = columns[0];
            GridViewColumn valuesColumn = columns[1];

            sensorsColumn.Width = 0.25 * sensorList.ActualWidth;
            valuesColumn.Width = sensorList.ActualWidth - sensorsColumn.Width - 10;
        }

        public void SensorsUpdated(object obj, EventArgs eventArgs)
        {
            /* Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                                       (DispatcherOperationCallback)delegate(object o)
            {
                sensorList.InvalidateVisual();
                return null;
            }, null); */
        }

    }
}
